$(document).ready(function()
    {
        var pinjaman = null;
        var total_pinjaman = 0, total_piutang = 0, total_tagihan = 0, sign = 0;
        
        $.calculate = function()
        {
            $('.currency').toNumber();
            total_tagihan = 0;
            for(var j = 0; j < pinjaman.length; j++)
                {
                    var current = pinjaman[j];
                    total_tagihan += $("#potongan_" + current.ID_PINJAMAN).val() * 1;
                }
            $("#LIST_POTONGAN tfoot").html("<tr>\n\
                                        <th colspan='5'>Total</th>\n\
                                        <th class='currency'>"+total_pinjaman+"</th>\n\
                                        <th class='currency'>"+total_piutang+"</th>\n\
                                        <th class='currency'>"+total_tagihan+"</th>\n\
                                        <th></th>\n\
                                        </tr>");
            $('.currency').formatCurrency();
        }

        $.notif_angsuran = function(data)
        {
            total_pinjaman = 0, total_piutang = 0, total_tagihan = 0, sign = 0;
            $("#LIST_POTONGAN tbody").html('');
            pinjaman = data.data.pinjaman;
            
            for(var j = 0; j < pinjaman.length; j++)
            {
                var current = pinjaman[j];

                $("#LIST_POTONGAN tbody").append("<tr>\n\
                                <td>"+j+1+"</td>\n\
                                <td>" + current.URAIAN_USAHA + "</td>\n\
                                <td>" + current.TGL_PINJAMAN + "</td>\n\
                                <td>" + current.LAMA_PINJAMAN + "/ " + current.ANGSURAN_KE+"</td>\n\
                                <td>" + current.BUNGA + "</td>\n\
                                <td class='currency'>" + current.JUMLAH_PINJAMAN + "</td>\n\
                                <td class='currency'>" + current.PIUTANG + "</td>\n\
                                <td><input class='currency' style='float:none; margin: 0 0 0 0; width:100px;' type='text' id='potongan_"+ current.ID_PINJAMAN +"' name='potongan_"+ current.ID_PINJAMAN +"'  value='" + current.POTONGAN + "' onchange='$.calculate()'/></td>\n\
                            </tr>");
                sign = 1;
                total_pinjaman += current.JUMLAH_PINJAMAN * 1;
                total_piutang +=  current.PIUTANG * 1;
                total_tagihan += current.POTONGAN * 1;
            }
            
            $("#LIST_POTONGAN tfoot").html("<tr>\n\
                                        <th colspan='5'>Total</th>\n\
                                        <th class='currency'>"+total_pinjaman+"</th>\n\
                                        <th class='currency'>"+total_piutang+"</th>\n\
                                        <th class='currency'>"+total_tagihan+"</th>\n\
                                        <th></th>\n\
                                        </tr>");
            $('.currency').formatCurrency();
        }

        $("#FORM_ANGSURAN").submit(function(){
            return false;
        });


        $("#Cari").click(function()
        {
            $.data_ajax('br-03/angsuran/anggota/', "#FORM_ANGSURAN", $.notif_angsuran);
        });

        $.notif_simpan = function(data)
        {
            alert(data.status);
            $('.currency').formatCurrency();
            window.location = base_url + "br-03/angsuran/edit";
        }

        $("#Simpan").click(function()
        {
            $('.currency').toNumber();
            $.data_ajax('br-03/angsuran/simpan/', "#FORM_ANGSURAN", $.notif_simpan);
        });
    });